package com.restaurant.application.root.commonui.data;

import com.google.common.collect.Lists;
import com.nebutown.generation.Convert;
import com.nebutown.generation.PickableBean;

import java.util.List;
import java.util.stream.Collectors;

public class EditorListValue implements PickableBean {

    public static final String FN_NAME = "name";
    public static final String FN_INSERTATTOPPATH = "insertAtTopPath";
    public static final String FN_ELEMENTS = "elements";

    public static final List<String> FIELD_NAMES = Lists.newArrayList(
            FN_NAME,
            FN_INSERTATTOPPATH,
            FN_ELEMENTS);

    private String name;
    private String insertAtTopPath;
    private List<EditorElement> elements;

    public EditorListValue(String name, String insertAtTopPath, List<EditorElement> elements) {
        setName(name);
        setInsertAtTopPath(insertAtTopPath);
        setElements(elements);
    }

    public EditorListValue() {
    }

    public EditorListValue duplicate() {
        EditorListValue result = new EditorListValue();
        result.name = this.name;
        result.insertAtTopPath = this.insertAtTopPath;
        result.elements = this.elements == null ? null : this.elements.stream().map(e5 -> e5 == null ? null : e5.duplicate()).collect(Collectors.toList());
        return result;
    }

    @Override
    public String toString() {
        return Convert.toJson(this);
    }

    public String getName() {
        return name;
    }

    public void setName(String value) {
        this.name = value;
    }

    public EditorListValue withName(String value) {
        setName(value);
        return this;
    }

    public EditorListValue withName(String value, String defaultValue) {
        setName(value == null ? defaultValue : value);
        return this;
    }

    public String getInsertAtTopPath() {
        return insertAtTopPath;
    }

    public void setInsertAtTopPath(String value) {
        this.insertAtTopPath = value;
    }

    public EditorListValue withInsertAtTopPath(String value) {
        setInsertAtTopPath(value);
        return this;
    }

    public EditorListValue withInsertAtTopPath(String value, String defaultValue) {
        setInsertAtTopPath(value == null ? defaultValue : value);
        return this;
    }

    public List<EditorElement> getElements() {
        if (elements == null) {
            elements = Lists.newArrayList();
        }
        return elements;
    }

    public void setElements(List<EditorElement> value) {
        this.elements = value;
    }

    public EditorListValue withElements(List<EditorElement> value) {
        setElements(value);
        return this;
    }

    public EditorListValue withElements(List<EditorElement> value, List<EditorElement> defaultValue) {
        setElements(value == null ? defaultValue : value);
        return this;
    }

    public EditorListValue addElements(EditorElement value) {
        if (elements == null) {
            elements = Lists.newArrayList();
        }
        this.elements.add(value);
        return this;
    }

    public Object getValueOf(String name) {
        switch (name) {
            case "name":
                return getName();
            case "insertAtTopPath":
                return getInsertAtTopPath();
            case "elements":
                return getElements();
        }
        return null;
    }

    public boolean hasField(String name) {
        return FIELD_NAMES.contains(name);
    }

    @Override
    public int hashCode() {
        return signature().hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (!(o instanceof EditorListValue)) return false;
        return signature().equals(((EditorListValue) o).signature());
    }

    public String signature() {
        throw new RuntimeException("Bean EditorListValue has no signature defined, no hash code or equals possible.");
    }
}
